import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.regex.Pattern;

class Console {
	private static final String SEPARATOR = System
			.getProperty("file.separator");
	static LinkedList q = new LinkedList();
	static final int MAX_SIZE = 5;

	static File curDir;
	private static String[] argArr;

	static {
		try {
			curDir = new File(System.getProperty("user.dir"));
		} catch (Exception e) {
		}
	}

	public static void main(String[] args) {
		// 1. 화면으로부터 라인단위로 입력받는다.
		Scanner in = new Scanner(System.in);
		while (true) {
			try {
				String prompt = curDir.getCanonicalPath() + ">>";

				System.out.print(prompt);

				// 2. 입력받은 문자를 가져온다.
				String input = in.nextLine();

				save(input);

				input.trim();

				argArr = input.split(" +");

				String command = argArr[0].trim();

				if (command == "")
					continue;

				command = command.toLowerCase();

				// q 또는 Q를 입력시 실행 종료
				if (input.equals("q") || input.equals("Q")) {
					System.out.println("프로그램을 종료합니다.");
					System.exit(0);
				} else if (command.equals("find")) {
					find();
				} else if (command.equals("find2")) {
					find2();
				} else if (command.equals("history")) {
					history();
				} else if (command.equals("dir")) {
					dir();
				} else if (command.equals("cd")) {
					cd();
				} else if (command.equals("type")) {
					type();
				} else {
					for (String str : argArr) {
						System.out.println(str);
					}
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} // while(true)
	} // main

	private static void cd() throws IOException {
		if (argArr.length == 1) {
			System.out.println(curDir);
			return;
		} else if (argArr.length > 2) {
			System.out.println("USAGE : cd directory");
			return;
		}

		String subDir = argArr[1];

		if (subDir.equals("..")) {
			curDir = curDir.getParentFile();
		} else if (subDir.equals(".")) {
			System.out.println(curDir.getCanonicalPath());
		} else {
			File tmp = new File(curDir.getCanonicalPath(), subDir);

			if (!tmp.exists()) {
				System.out.println("유효하지 않은 디렉토리 입니다.");
			} else {
				curDir = tmp;
			}
		}
	}

	private static void find2() throws IOException {
		// TODO Auto-generated method stub
		if (argArr.length != 3) {
			System.out.println("Usage : find2 KEYWORD FILE_NAME");
			return;
		}

		String pattern = argArr[2];

		// 정규식으로 치환
		pattern = pattern.replace(".", "\\.");
		pattern = pattern.replace("*", ".*");
		pattern = pattern.replace("?", ".{1}");

		Pattern p = Pattern.compile(pattern);

		File[] FileList = curDir.listFiles();
		for (int i = 0; i < FileList.length; i++) {
			File file = FileList[i];

			if (!p.matcher(file.getName()).matches())
				continue;

			if (!file.isDirectory()) {
				System.out.println("-------------" + file.getName());

				find(file.getName());
			}

		}
	}

	private static void find() throws IOException {
		// TODO Auto-generated method stub
		if (argArr.length != 3) {
			System.out.println("Usage : find KEYWORD FILE_NAME");
			return;
		}

		String fileName = argArr[2];

		find(fileName);
	}

	private static void find(String filename) throws IOException {
		File tmp = new File(curDir, filename);

		if (tmp.exists()) {
			if (tmp.isDirectory()) {
				System.out.println(tmp.getName() + "는 파일이 아닙니다.");
			} else {
				FileReader reader = new FileReader(tmp);
				BufferedReader buffer = new BufferedReader(reader);

				String pattern = argArr[1];

				// 정규식으로 치환
				pattern = pattern.replace(".", "\\.");
				pattern = pattern.replace("*", ".*");
				pattern = pattern.replace("?", ".{1}");

				Pattern p = Pattern.compile(".*" + pattern + ".*");

				int lineNum = 1;
				String line;
				while ((line = buffer.readLine()) != null) {
					if (p.matcher(line).matches())
						System.out.println(lineNum++ + " " + line);
				}
			}
		} else {
			System.out.println("파일이 존재하지 않습니다.");
		}
	}

	private static void type(String filename) throws IOException {
		String fileName = filename;

		File tmp = new File(curDir, fileName);

		if (tmp.exists()) {
			if (tmp.isDirectory()) {
				System.out.println(tmp.getName() + "는 파일이 아닙니다.");
			} else {
				FileReader reader = new FileReader(tmp);
				BufferedReader buffer = new BufferedReader(reader);

				int lineNum = 1;
				String line;
				while ((line = buffer.readLine()) != null) {
					System.out.println(lineNum++ + " " + line);
				}
			}
		} else {
			System.out.println("파일이 존재하지 않습니다.");
		}

		return;
	}

	private static void type() throws IOException {
		if (argArr.length != 2) {
			System.out.println("Usage : type FILE_NAME");
			return;
		}

		type(argArr[1]);
	}

	private static void dir() {
		// TODO Auto-generated method stub
		String pattern = "";
		File[] FileList;

		switch (argArr.length) {
		case 1:
			FileList = curDir.listFiles();
			for (int i = 0; i < FileList.length; i++) {
				File file = FileList[i];
				if (file.isDirectory())
					System.out.println("[" + file.getName() + "]");
				else
					System.out.println(file.getName());
			}

			break;

		case 2:
			pattern = argArr[1];

			// 정규식으로 치환
			pattern = pattern.replace(".", "\\.");
			pattern = pattern.replace("*", ".*");
			pattern = pattern.replace("?", ".{1}");

			Pattern p = Pattern.compile(pattern);

			FileList = curDir.listFiles();
			for (int i = 0; i < FileList.length; i++) {
				File file = FileList[i];

				if (!p.matcher(file.getName()).matches())
					continue;

				if (file.isDirectory())
					System.out.println("[" + file.getName() + "]");
				else
					System.out.println(file.getName());
			}

			break;

		default:
			System.out.println("USAGE : dir [FILENAME]");
		}
	}

	private static void save(String input) {
		// TODO Auto-generated method stub
		if (input == null || "".equals(input))
			return;

		int cnt = q.size();

		if (cnt > MAX_SIZE)
			q.pollFirst();

		q.add(input);
	}

	private static void history() {
		// TODO Auto-generated method stub
		int cnt = q.size();

		for (int i = 0; i < cnt; i++) {
			if ((i + 1) > MAX_SIZE)
				break;
			System.out.println((i + 1) + " " + q.get(i));
		}

	}
} // class